package cn.mntool.backup.utils;

import cn.mntool.backup.config.BackupConfig;
import cn.mntool.backup.entity.BackupTimeProperties;
import cn.mntool.backup.entity.CommandProperties;
import org.apache.commons.lang3.SystemUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * describe:最后备份时间读取工具
 *
 * @author lianying
 * @date 2019/01/31
 */
public class LastTimeUtils {
    /**
     * log4j
     */
    private static final Logger logger= Logger.getLogger(LastTimeUtils.class);
    /**
     * 读取配置文件
     */
    private static Properties properties=new Properties();
    private static final String SETTINGS="lastTime.properties";
    static {
        try {
            properties.load(new FileInputStream(new File(PathUtils.classPath()+SETTINGS)));
        } catch (Exception e) {
            logger.info("读取配置信息异常",e);
        }
    }
    /**
     * 重新读取配置
     */
    public static void reload()
    {
        try {
            properties.load(new FileInputStream(new File(PathUtils.classPath()+SETTINGS)));
            logger.info("重新读取配置信息成功");
        } catch (Exception e) {
            logger.info("重新读取配置信息异常",e);
        }
    }

    /**
     * 通过key获得值
     *
     * @param key
     * @return
     */
    public static String get(String key)
    {
        return properties.getProperty(key);
    }

    /**
     * 设置参数
     *
     * @param key
     * @param value
     */
    public static void update(String key,String value)
    {
        try {

            // 从输入流中读取属性列表（键和元素对）
            properties.setProperty(key, value);
            FileOutputStream outputFile = new FileOutputStream(PathUtils.classPath()+SETTINGS);
            properties.store(outputFile, null);
            outputFile.flush();
            outputFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
